package de.szut.dqi10.brailletouchscreenreader.display;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import de.szut.dqi10.brailletouchscreenreader.braillechar.BrailleChar;

/**
 * View displayed in BrailleMainActivity
 * @author Marco Jedzig, Simon Grafenhorst, Felix Pusch
 *
 */
public class BrailleView extends View {

	private static final String TAG = "BrailleView";

	private static final int ACTIVATED_COLOR_RED = 255;
	private static final int ACTIVATED_COLOR_GREEN = 255;
	private static final int ACTIVATED_COLOR_BLUE = 255;

	private static final int DEACTIVATED_COLOR_RED = 150;
	private static final int DEACTIVATED_COLOR_GREEN = 150;
	private static final int DEACTIVATED_COLOR_BLUE = 220;

	private static final int BACKGROUND_COLOR_RED = 0;
	private static final int BACKGROUND_COLOR_GREEN = 0;
	private static final int BACKGROUND_COLOR_BLUE = 0;

	private int scaleX = 100;
	private int scaleY = 80;

	private BrailleChar brailleChar;
	private BrailleTouchListener touchListener;
	private Paint activatedPaint;
	private Paint deactivatedPaint;
	private Paint backgroundPaint;
	private int vibrateMode;

	/**
	 * Calls the constructor of the super class and sets the onTouchListener to
	 * a BrailleTouchListener
	 * 
	 * @param context
	 * 
	 * @param attrs
	 */
	public BrailleView(Context context, AttributeSet attrs) {
		super(context, attrs);

		Log.i(TAG, "Created: " + TAG);

		init();
		setOnTouchListener(touchListener);
	}

	/**
	 * Initializes all variables and creates paints for activated and
	 * deactivated buttons as well as the background paint. Also creates a new
	 * BrailleTouchListener
	 */
	private void init() {
		Log.i(TAG, "Initializing");

		touchListener = new BrailleTouchListener(brailleChar, vibrateMode,
				scaleX, scaleY, true);
		activatedPaint = new Paint();
		activatedPaint.setARGB(255, ACTIVATED_COLOR_RED, ACTIVATED_COLOR_GREEN,
				ACTIVATED_COLOR_BLUE);
		deactivatedPaint = new Paint();
		deactivatedPaint.setARGB(255, DEACTIVATED_COLOR_RED,
				DEACTIVATED_COLOR_GREEN, DEACTIVATED_COLOR_BLUE);
		backgroundPaint = new Paint();
		backgroundPaint.setARGB(255, BACKGROUND_COLOR_RED, BACKGROUND_COLOR_GREEN,
				BACKGROUND_COLOR_BLUE);
	}

	/**
	 * Sets the vibrateMode
	 * 
	 * @param vibrateMode
	 *            The vibrate mode
	 */
	public void setVibrateMode(int vibrateMode) {
		Log.i(TAG, "setVibrateMode: " + String.valueOf(vibrateMode));

		this.vibrateMode = vibrateMode;
		touchListener.setVibrateMode(this.vibrateMode);
	}

	/**
	 * Scaling the view and then moving it to the top left corner to make the
	 * characters readable without moving the hand too much
	 * 
	 * @param fullscreen
	 *            Displays in fullscreen if true and in the top left corner if
	 *            false
	 */
	public void setFullscreen(boolean fullscreen) {
		Log.i(TAG, "setFullscreen: " + fullscreen);
		if (!fullscreen) {
			setScaleX(0.5F); //Scale first
			setScaleY(0.5F);
			setPivotX(0); // Then pivot
			setPivotY(0);
		} else {
			setScaleX(1);
			setScaleY(1);
		}
	}

	/**
	 * 
	 * @param scaleX
	 *            The horizontal scale of the buttons
	 * 
	 * @param scaleY
	 *            The vertical scale of the buttons
	 */
	public void setScale(int scaleX, int scaleY) {
		Log.i(TAG, "setScale: " + scaleX + ", " + scaleY);
		this.scaleX = scaleX;
		this.scaleY = scaleY;
		touchListener.setButtonSize(scaleX, scaleY);
		invalidate();
	}

	public void setAutonext(boolean autonext) {
		Log.i(TAG, "setAutonext: " + autonext);
		touchListener.setAutoNextChar(autonext);
	}

	/**
	 * Sets the brailleChar to display and calls the setBrailleChar method of
	 * the touchListener. Also repaints the view to display the new BrailleChar
	 * correctly.
	 * 
	 * @param brailleChar
	 *            The brailleChar to display
	 */
	public void setBrailleChar(BrailleChar brailleChar) {
		Log.i(TAG, "setBrailleChar: " + brailleChar);

		this.brailleChar = brailleChar;

		touchListener.setBrailleChar(brailleChar);

		invalidate();
	}

	/**
	 * Calls the displayBraille method to visualize the brailleChar
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		Log.i(TAG, "drawing");

		displayBraille(canvas);
	}

	/**
	 * Visualizes the brailleChar with the given paints and borders. Draws six
	 * rectangles to represent the six points of the braille characters. 
	 * 
	 * @param canvas The canvas to draw on
	 */
	private void displayBraille(Canvas canvas) {
		canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);
		int borderX = ((int) (float) getWidth() / 2 - getWidth() / 2 * scaleX
				/ 100) / 2;
		int borderY = ((int) (float) getHeight() / 3 - getHeight() / 3 * scaleY
				/ 100) / 2;
		for (int i = 0; i < 6; i++) {
			float startX, startY, endX, endY;
			if ((int) ((brailleChar.getBrailleChar() >> i) % 2) == 1) {
				startX = getWidth() / 2 * (int) (i / 3) + borderX;
				startY = getHeight() / 3 * (int) (i % 3) + borderY;
				endX = getWidth() / 2 * (int) (i / 3 + 1) - borderX;
				endY = getHeight() / 3 * (int) (i % 3 + 1) - borderY;
				canvas.drawRect(startX, startY, endX, endY, activatedPaint);
			} else {
				startX = getWidth() / 2 * (int) (i / 3) + borderX;
				startY = getHeight() / 3 * (int) (i % 3) + borderY;
				endX = getWidth() / 2 * (int) (i / 3 + 1) - borderX;
				endY = getHeight() / 3 * (int) (i % 3 + 1) - borderY;
				canvas.drawRect(startX, startY, endX, endY, deactivatedPaint);
			}
		}
	}
}
